package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.JiguanInfo;
import ywpt.web.common.PublicFunction;
@Service("jiguanService")
@RemoteProxy(name="jiguanService")
public class JiguanServiceImpl extends HibernateEntityDao<JiguanInfo> implements JiguanService{
	public  JiguanServiceImpl() {
		
	}

	@Override
	public JiguanInfo getJiguanInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public List<JiguanInfo> getJiguanInfos() {
		return this.getAll();
	}

	@Override
	public String createJiguanCode() {
		DetachedCriteria query = DetachedCriteria.forClass(JiguanInfo.class, "jiguan");
		String jiguanCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<JiguanInfo> jiguanInfos = this.getJiguanInfos(query);
		if ( jiguanInfos.iterator().hasNext() )	
		{
			code = jiguanInfos.iterator().next().getCode();
			maxId = new Integer(code);
		}
		jiguanCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return jiguanCode;
	}

	@Override
	public int getJiguanInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<JiguanInfo> getJiguanInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public void removeJiguanInfo(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveJiguanInfo(JiguanInfo jiguanInfo) {
		this.save(jiguanInfo);
	}

	@Override
	public void updateJiguanInfo(JiguanInfo jiguanInfo) {
		this.update(jiguanInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<JiguanInfo> getJiguanInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<JiguanInfo> getJiguanByJiguanName(String jiguanName, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(JiguanInfo.class, "jiguan");
		if ( jiguanName != null && !jiguanName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("place", "%" + jiguanName + "%"),  
							          Restrictions.like("code", "%" + jiguanName + "%")));
		}
		query.addOrder(Order.asc("id"));
		List<JiguanInfo> jiguanInfoList = new ArrayList<JiguanInfo>();
		List<JiguanInfo> jiguanInfoLists = new ArrayList<JiguanInfo>();
		JiguanInfo jiguanInfo =new JiguanInfo();
		
		jiguanInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<JiguanInfo> itr = jiguanInfoList.iterator(); itr.hasNext(); i++ ) 
		{
			jiguanInfo = itr.next();
			String ss = PublicFunction.format(jiguanInfo.getCode() + "",5, "  ", 1)
			          + jiguanInfo.getPlace(); 
			
			jiguanInfo.setPlace(ss);
			jiguanInfoLists.add(jiguanInfo);
		}
		return jiguanInfoLists;
	}

	@Override
	@RemoteMethod
	public int getJiguansCountByName(String jiguanName) {
		DetachedCriteria query = DetachedCriteria.forClass(JiguanInfo.class, "jiguan");
		if ( jiguanName != null && !jiguanName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("place", "%" + jiguanName + "%" ),  
							          Restrictions.like("code", "%" + jiguanName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}
}
